Array.prototype.flat = function (deep = 1) {
  function flat(array, deep) {
    const arr = []
    array.forEach(it => {
      if (deep-- > 0 && Array.isArray(it)) {
        arr.push(...flat(it, deep))
      } else {
        arr.push(it)
      }
      deep++
    })
    return arr
  }
  return flat(this, deep)
}



Array.prototype.flat = function (deep = 1) {
  return this.reduce((acc, item) => {
    let newDeep = deep - 1
    if (newDeep >= 0 && Array.isArray(item)) {
      acc.push(...item.flat(newDeep))
    }
    else {
      acc.push(item)
    }
    return acc
  }, [])
}

[1, 2, 3, [4, 5, [6, 7, [8]]], 9, 10].flat()
